<script lang="ts" setup>
import { onMounted } from 'vue';
import { clearTokenForClient, clearAtomGitUserForClient } from '@/common/utils/auth';
import { $fetch } from '@/passport/utils/fetch';

const props = defineProps({
  from: {
    type: String,
    default: '/',
  },
});

onMounted(async () => {
  // 检查 from 合法性，只支持站内地址
  if (!props.from.startsWith('/')) {
    window.location.href = '/';

    return;
  }

  const base = `${window.location.protocol}//${window.location.host}`;
  const redirect = `${base}${props.from}`;

  // 拼接登出地址
  const logoutPath = `${import.meta.env.VITE_ATOMGIT_AUTH_URL}/login/profile/logout?redirect_uri=${encodeURIComponent(redirect)}`;

  clearTokenForClient();
  clearAtomGitUserForClient();

  if (!import.meta.env.DEV) {
    // 清理 AtomGit 的 Cookie
    await $fetch('/passport/index/clear-atomgit-token', {
      baseURL: '/',
    });
  }

  window.location.href = logoutPath;
});
</script>

<template>
  <main>
    正在登出，请稍候...
  </main>
</template>
